//
//  EnvironmentManage.m
//  BaseProject
//
//  Created by Macx on 2018/2/23.
//  Copyright © 2018年 Gavin. All rights reserved.
//

#import "EnvironmentManage.h"
#import "ProjectAPI.h"
#import <UIKit/UIKit.h>
#import "NetworkConfig.h"

@interface EnvironmentManage()

@property(nonatomic ,assign) EnvironmentType environmentType;    //环境类型

@end

@implementation EnvironmentManage

+(EnvironmentManage *)sharedEnvirManage{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    
    return sharedInstance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        //非线上环境
        if (Environment == 0) {
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSNumber *type = [userDefaults objectForKey:@"environmentType"];
//            _environmentType = type ? [type integerValue] :(isDevEnvironment?DevEnvironment:TestEnvironment);//没有保存过默认选择一个环境
            _environmentType = isDevEnvironment?DevEnvironment:TestEnvironment;//不使用上次保存的环境(如果上次使用了其他环境,然后把切换环境按钮隐藏了,就使用不了开发环境了)
        }
        
        //线上环境
        if (Environment == 1) {
            _environmentType = OnlineEnvironment;
        }
    }
    return self;
}


- (NSString *)getBaseAPIUrl{
    
    NSString *url = @"";
    
    if (_environmentType == DevEnvironment) {//开发
        
        url = base_global_url_dev;
    }

    if (_environmentType == TestEnvironment) {//测试

        url = base_global_url_test;
    }
    
    if (_environmentType == preReleaseEnvironment) {//预生产
        
        url = base_global_url_pre;
    }
    
    if (_environmentType == OnlineEnvironment) {//线上
        
        url = base_global_url_online;
    }
    NSLog(@"------>当前环境:%@",url);
    return url;
}

//获取H5-》API
- (NSString *)getWebBaseAPIUrl{
    
    NSString *url = @"";
    
    if (_environmentType == DevEnvironment) {//开发
        
        return base_web_url_dev;
    }
    
    if (_environmentType == TestEnvironment) {//测试
        
        return base_web_url_test;
    }
    
    if (_environmentType == preReleaseEnvironment) {//预生产
        
        url = base_web_url_pre;
    }
    
    if (_environmentType == OnlineEnvironment) {//线上
        
        url = base_web_url_online;
    }
    
    return url;
}


//获取微信公众号API
- (NSString *)getWxBaseAPIUrl{
    
    NSString *url = @"";

    return url;

}


- (NSString *)getCurrentEnvironment{
    
    NSString *str = @"测试环境";
    
    if (_environmentType == DevEnvironment) {//开发
        
        return @"开发环境";
    }
    
    if (_environmentType == TestEnvironment) {//测试
        
        return @"测试环境";
    }
    
    if (_environmentType == preReleaseEnvironment) {//预生产
        
        str = @"验收环境";
    }
    
    if (_environmentType == OnlineEnvironment) {//线上
        
        str = @"线上环境";
    }
    
    return str;
}

//获取环境类型
- (EnvironmentType)getCurrentEnvironmentType{
    
    return _environmentType;
}

//切换环境
- (void)changeEnvironment:(EnvironmentType)type{
    _environmentType = type;
    [[NSUserDefaults standardUserDefaults] setObject:@(_environmentType) forKey:@"environmentType"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    NSLog(@"\n----->切换为:%@\n",[self getCurrentEnvironment]);
    
    [NetworkConfig sharedConfig].baseUrl = [self getBaseAPIUrl];
    NSLog(@"----->切换后的域名:%@",[self getBaseAPIUrl]);
//    [self exitApplication];
}

- (void)exitApplication {
    [ProgressHUDManage showLoading:@""];
    //UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [UIView animateWithDuration:0.1 delay:2.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        //[window setAlpha:0];
    } completion:^(BOOL finished) {
        [ProgressHUDManage hideTips];
        abort();
    }];
}



@end
